package com.tys.algorithm.beginner;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
// 测试链接：https://leetcode.com/problems/binary-tree-level-order-traversal-ii

public class Code27_BinaryTreeLevelOrderTraversalII {
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }

    //从底部按层遍历
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        //双端队列
        List<List<Integer>> ans = new LinkedList<>();
        if (root == null) {
            return ans;
        }

        //接口Queue
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> curAns = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                TreeNode curNode = queue.poll();
                curAns.add(curNode.val);
                if (curNode.left != null) {
                    queue.add(curNode.left);
                }
                if (curNode.right != null) {
                    queue.add(curNode.right);
                }
            }
            ans.add(0, curAns);
        }
        return ans;
    }
}
